<template>
  <div @click="$emit('click')">
    <view class="entire">
      <image class="image" mode="aspectFill" :src="imageSrc" @error="handleImageError" />
      <view class="title">{{ title }}</view>
    </view>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const props = defineProps({
  title: { type: String, required: true },
  picture: { type: String, required: true },
})

const defaultImage = '/static/images/icons/default.jpg'
const imageSrc = ref(props.picture)

// 处理图片加载失败
const handleImageError = () => {
  imageSrc.value = defaultImage
}
</script>

<style scoped lang="scss">
.entire {
  margin-left: 15rpx;
  height: 160rpx;
  width: 300rpx;
  display: flex;
  position: relative;
  overflow: hidden;
  border-radius: 15rpx;
  .image {
    height: 160rpx;
    width: 300rpx;
    z-index: -10;
  }
  .title {
    position: absolute;
    bottom: 0rpx;
    z-index: 10;
    height: 40rpx;
    width: 300rpx;
    padding-left: 20rpx;
    background-color: rgba(0, 0, 0, 0.47);
    font-size: small;
    color: white;
    overflow: hidden; /* 隐藏溢出的内容 */
    white-space: nowrap; /* 保持文本在一行内 */
    text-overflow: ellipsis; /* 溢出内容显示省略号 */
  }
}
</style>
